#include "StdAfx.h"
#include "Utility.h"
#include "CustomComboBox.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomComboBox, CComboBox)

CCustomComboBox::CCustomComboBox(void)
{
	//common Parameter
	m_nState = CBB_DEFAULT;

	//String and Font Parameter
	m_strText=_T("");
	m_nFontSize = 11;
	m_nFontStyle = FontStyleBold;
	m_enFontUnit = UnitPoint;
	m_strTextFormat = NULL;
	m_pFontFamily = NULL;
	m_pFontBrush = NULL;
	m_pFont = NULL;

	//Image and Handle Parameter
	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;
	for(int i = 0; i < 4; ++i)
		m_pImg[i] = NULL;
}

CCustomComboBox::~CCustomComboBox(void)
{
	if(m_pFont != NULL){ delete m_pFont;  m_pFont = NULL; }
	if(m_pFontBrush != NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	if(m_pFontFamily != NULL){ delete m_pFontFamily;  m_pFontFamily = NULL; }
	if(m_strTextFormat != NULL){ delete m_strTextFormat;  m_strTextFormat = NULL; }	

	for(int i = 0; i < 4; ++i)
		if ( m_pImg[i] )
			delete m_pImg[i];
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
int CCustomComboBox::SetCurSel(int nSelect)
{
	int nTemp = CComboBox::SetCurSel(nSelect);
	InvalidateRect(NULL, FALSE);
	return nTemp;
}

void CCustomComboBox::SetState(int nState)
{
	if(m_nState != nState){ m_nState = nState;  InvalidateRect(NULL); }//Invalidate(); }
}

BOOL CCustomComboBox::EnableWindow(BOOL bEnable)
{
	if(bEnable)
		SetState(CBB_DEFAULT);
	else
		SetState(CBB_DISABLED);

	InvalidateRect(NULL, FALSE);
	return CComboBox::EnableWindow(bEnable);
}

void CCustomComboBox::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Image Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomComboBox::SetImage(UINT nDisabledID, UINT nNormalID, UINT nHoveredID, UINT nPressedID)
{
	CUtility::ImageFromIDResource(nDisabledID, _T("PNG"), m_pImg[0]);
	CUtility::ImageFromIDResource(nNormalID, _T("PNG"), m_pImg[1]);
	CUtility::ImageFromIDResource(nHoveredID, _T("PNG"), m_pImg[2]);
	CUtility::ImageFromIDResource(nPressedID, _T("PNG"), m_pImg[3]);
}

void CCustomComboBox::SetImage(LPCTSTR pszDisabled, LPCTSTR pszNormal, LPCTSTR pszHovered, LPCTSTR pszPressed)
{
	if ( pszDisabled )
		m_pImg[0] = Image::FromFile( pszDisabled );

	if ( pszNormal )
		m_pImg[1] = Image::FromFile( pszNormal );

	if ( pszHovered )
		m_pImg[2] = Image::FromFile( pszHovered );

	if ( pszPressed )
		m_pImg[3] = Image::FromFile( pszPressed );
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//String Font Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomComboBox::SetFontSize(int nSize)
{
	if(nSize > 0)
		m_nFontSize = nSize;
}

void CCustomComboBox::SetFontStyle(UINT nStyle)
{
	if(nStyle >= 0)
		m_nFontStyle = nStyle;
}

void CCustomComboBox::SetFontColor(int nR, int nG, int nB)
{
	if(m_pFontBrush!=NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	m_pFontBrush = new SolidBrush(Color(247, nR, nG, nB));	

}
void CCustomComboBox::SetFontFamily(CString strFontFamilyName)
{
	if(!strFontFamilyName.IsEmpty())
	{
		if(m_pFontFamily!=NULL){ delete m_pFontFamily;  m_pFontFamily=NULL; }
		m_pFontFamily = new FontFamily(strFontFamilyName);
	}
}

void CCustomComboBox::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new FontFamily(L"Arial");
	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new Font
	if(m_pFont == NULL)
		m_pFont = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSize, m_nFontStyle, m_enFontUnit);

	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentCenter);
		m_strTextFormat->SetLineAlignment(StringAlignmentCenter);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}

	//----------------------new FontBrush
	if (m_pFontBrush == NULL)
		m_pFontBrush = new SolidBrush(Color(247, 247, 247, 247));	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomComboBox::DrawUI()
{
	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);

	Graphics graphics( m_hDrawDC );
	graphics.DrawImage(m_pImg[m_nState], m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height());

	GetWindowText(m_strText);
	if(!m_strText.IsEmpty())
	{
		CreateFont();
		graphics.DrawString(m_strText.GetBuffer(), -1, m_pFont, 
			                RectF((Gdiplus::REAL)m_rtRect.left,    (Gdiplus::REAL)m_rtRect.top,
			                      (Gdiplus::REAL)m_rtRect.Width() - 14, (Gdiplus::REAL)m_rtRect.Height()),
			                m_strTextFormat, m_pFontBrush);
	}

	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}

void CCustomComboBox::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	DrawUI();
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomComboBox, CComboBox)
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
	ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	ON_CONTROL_REFLECT(CBN_CLOSEUP, OnCloseUp)
	ON_WM_PAINT()
END_MESSAGE_MAP()

void CCustomComboBox::PreSubclassWindow()
{
	if(!IsWindowEnabled())
		m_nState = CBB_DISABLED;

	ModifyStyle(0, BS_OWNERDRAW);
	CComboBox::PreSubclassWindow();
}

BOOL CCustomComboBox::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CCustomComboBox::OnMouseMove(UINT nFlags, CPoint point)
{
	TRACKMOUSEEVENT tme;
	tme.cbSize = sizeof(tme);
	tme.hwndTrack = m_hWnd;
	tme.dwFlags = TME_LEAVE|TME_HOVER;
	tme.dwHoverTime = 1;
	_TrackMouseEvent(&tme);

	CComboBox::OnMouseMove(nFlags, point);
}

LRESULT CCustomComboBox::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
	if(m_nState != CBB_DISABLED) 
		SetState(CBB_DEFAULT);
	return 0;
}

LRESULT CCustomComboBox::OnMouseHover(WPARAM wparam, LPARAM lparam) 
{
	if(m_nState != CBB_DISABLED) 
		SetState(CBB_HOVERED);
	return 0;
}

void CCustomComboBox::OnDropdown()
{
	if(m_nState != CBB_DISABLED) 
		SetState(CBB_PRESSED);

	InvalidateRect(NULL, FALSE);
}

void CCustomComboBox::OnCloseUp()
{
	if(m_nState != CBB_DISABLED) 
		SetState(CBB_DEFAULT);

	InvalidateRect(NULL, FALSE);
}
